<?php

namespace app\api\controller;

use app\admin\model\pes\frame\Partners;
use app\admin\model\pes\freight\Price;
use app\admin\model\pes\Locations;
use app\admin\model\pes\transportation\Type;
use app\admin\model\pes\vehicle\city\Volumes;
use app\admin\model\pes\vehicle\Models;
use app\api\validate\pes\freight\PriceValidate;
use app\common\controller\Api;
use App\Models\TransportationType;

/**
 * 首页接口
 */
class Location extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {

        $data     = $this->request->param();
        $validate = new PriceValidate();
        if (!$validate->scene('register')->check($data)) {
            return json(['error' => $validate->getError()]);
        }

        // 查询始发地和目的地
        $origin      = Locations::where('id', $data['origin_id'])->find();
        $destination = Locations::where('id', $data['destination_id'])->find();
        if (!$origin || !$destination) {
            return json(['error' => '始发地或目的地不存在'], 404);
        }
        // 查询运输类型，例如电动车或摩托车
        $transportationType = Type::where('id', $data['transportation_type_id'])->find();
        if (!$transportationType) {
            $this->error(__('无效的运输类型'));
        }


        // 查询运费价格
        $price = Price::where('origin_id', $origin->id)
                      ->where('destination_id', $destination->id)
                      ->where('transportation_type_id', $transportationType->id)
                      ->find();

        if (!$price) {
            $this->error(__('没有找到相应的运费报价'));
//            return json(['error' => '没有找到相应的运费报价'], 404);
        }

        // 如果是电动车，直接按台计算
        if ($transportationType['type'] === 1) {
            $this->success('', [
                'origin'      => $origin->name,
                'destination' => $destination->name,
                'price'       => $price->price
            ]);
        }
        $partners = Partners::where('id', $origin->id)->find();
        if (!$partners) {
            $this->error(__('未找到木架服务商'));

        }
        // 查询车型的裸车体积和 1.4 倍系数体积
        $vehicle = Models::where('id', $data['vehicles_id'] ?? '')->find();
        if (!$vehicle) {
            $this->error(__('未找到该车型'));
        }
        // 将所有浮点数转换为字符串，因为BCMath函数需要字符串参数
        // 将浮点数转换为字符串
        $coefficient = '1.4';

        // 使用BCMath函数进行计算
        // 首先计算括号内的和
        $sum = bcadd(
            $price->price,
            $partners->cost_per_cubic_meter,
            10 // 设置精度为10位小数
        );

        // 然后乘以车辆的裸体积
        $product = bcmul(
            $sum,
            $vehicle->bare_volume,
            10 // 设置精度为10位小数
        );

        // 最后乘以系数
        $cost = bcmul(
            $product,
            $coefficient,
            10 // 设置精度为10位小数
        );
        $this->success('', [
            'origin'             => $origin->name,
            'destination'        => $destination->name,
            'coefficient_volume' => $vehicle->coefficient_volume,
            'price'              => $cost
        ]);


//        return json(['error' => '未知的车型类型'], 400);
//
//        return $price ? response()->json($price) : response()->json(['message' => '未找到运费信息'], 404);
    }
}
